﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.UI.Models;
using WebNext.UI.Models.Login;
using WebNext.UI.Properties;
using WebNext.UI.Utils;
using WebNext.Domain;
using WebNext.Domain.Interfaces;
using WebNext.Domain.Entities;
using WebNext.UI.Models.Admin;

namespace WebNext.UI.Controllers
{
	public class AdminController : BaseController
	{
		private IAdminSettingsRepository adminRepository;
		private IAuthorInfoRepository authorInfoRepository;

		public AdminController(IAdminSettingsRepository adminRepository, IAuthorInfoRepository authorInfoRepository)
		{
			this.adminRepository = adminRepository;
			this.authorInfoRepository = authorInfoRepository;
			this.authorInfoRepository.CultureId = this.CultureId;
		}

		[Authorize]
		public ActionResult Edit()
		{
			AdminSettings adminSettings = this.adminRepository.GetSettings();
			if (adminSettings == null)
			{
				this.TempData[Constants.Message] = Resources.AdminSettingsNotExist;
				return this.RedirectToAction("List", "Entries", new { viewType = MainViewType.Blog, pageNumber = 1 });
			}
			AdminSettingsViewModel adminSettingsViewModel = new AdminSettingsViewModel(adminSettings);

			if (this.TempData[Constants.Temp] is AuthorInfoViewModel)
			{
				adminSettingsViewModel.AuthorInfo = this.TempData[Constants.Temp] as AuthorInfoViewModel;
			}
			else
			{
				AuthorInfo authorInfo = this.authorInfoRepository.GetAuthorInfo();
				if (authorInfo != null)
				{
					adminSettingsViewModel.AuthorInfo = new AuthorInfoViewModel(authorInfo);
				}
			}
			return this.View(adminSettingsViewModel);
		}

		[HttpPost]
		[ValidateInput(false)]
		[Authorize]
		public ActionResult Edit(AdminSettingsViewModel adminSettingsViewModel)
		{
			ValidationResult result = adminSettingsViewModel.Validate();
			if (result.IsValid)
			{
				AdminSettings adminSettings = this.adminRepository.GetSettings();
				if (adminSettings == null)
				{
					this.TempData[Constants.Message] = Resources.AdminSettingsNotExist;
					return this.RedirectToAction("List", "Entries", new { viewType = MainViewType.Blog, pageNumber = 1 });
				}

				adminSettings.UserName = adminSettingsViewModel.UserName;
				adminSettings.Email = adminSettingsViewModel.Email;
				if (adminSettingsViewModel.Password != AdminSettings.PasswordStub)
				{
					PasswordHelper passwordHelper = new PasswordHelper();
					adminSettings.Password = passwordHelper.Hash(adminSettingsViewModel.Password);
				}
				this.TempData[Constants.Message] = Properties.Resources.AdminSettingsSaved;
			}
			else
			{
				this.TempData[Constants.Message] = result.GetErrorsMessage();
			}

			AuthorInfo authorInfo = this.authorInfoRepository.GetAuthorInfo();
			if (authorInfo != null)
			{
				adminSettingsViewModel.AuthorInfo = new AuthorInfoViewModel(authorInfo);
			}

			return this.View(adminSettingsViewModel);
		}

		[HttpPost]
		[ValidateInput(false)]
		[Authorize]
		public ActionResult EditAuthorInfo(AuthorInfoViewModel authorInfoViewModel)
		{
			ValidationResult result = authorInfoViewModel.Validate();
			if (result.IsValid)
			{
				AuthorInfo authorInfo = this.authorInfoRepository.GetAuthorInfo();
				if (authorInfo == null)
				{
					this.TempData[Constants.Message] = Resources.AuthorSettingsNotExist;
				}
				else
				{
					authorInfo.Name = authorInfoViewModel.Name;
					authorInfo.Description = authorInfoViewModel.Description;
					authorInfo.SocialNetwork = authorInfoViewModel.SocialNetwork;
					authorInfo.CustomScript = authorInfoViewModel.CustomScript;
					authorInfo.ImageUri = authorInfoViewModel.ImageUri;
					this.TempData[Constants.Message] = Properties.Resources.AuthorSettingsSaved;
				}
			}
			else
			{
				this.TempData[Constants.Message] = result.GetErrorsMessage();
				this.TempData[Constants.Temp] = authorInfoViewModel;
			}
			return this.RedirectToAction("Edit");
		}

		public ActionResult AuthorInfoView()
		{
			AuthorInfo authorInfo = this.authorInfoRepository.GetAuthorInfo();
			if (authorInfo != null)
			{
				AuthorInfoViewModel authorInfoViewModel = new AuthorInfoViewModel(authorInfo);
				return this.View(authorInfoViewModel);
			}
			return this.View(null as AuthorInfoViewModel);
		}
	}
}